<template>
  <div>
    <el-upload class="avatar-uploader" :action="action" :show-file-list="false" :on-success="handleUploaderSuccess" :headers="headers" :accept="accept" :multiple="multiple">
      <div v-if="imgUrl" >
        <img class="el-upload-list__item-thumbnail avatar"  :src="imgUrl" alt="">
        <span class="el-upload-list__item-actions">
          <span class="el-upload-list__item-preview" @click.stop="handlePictureCardPreview"><i class="el-icon-zoom-in"></i></span>
          <span class="el-upload-list__item-delete" @click.stop="handleRemove"><i class="el-icon-delete"></i></span>
        </span>
      </div>
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogImgVisible" append-to-body>
      <img width="100%" :src="imgUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  props: {
    imgUrl:{
      type:String,
      default:''
    },
    //是否支持多选文件	
    multiple:{
      type:Boolean,
      default:false
    },
    //允许上传类型
    accept:{
      type:String,
      default: ".jpg,.png,.jpeg",
    },
  },
  data () {
    return {
      dialogImgVisible:false
    }
  },
  computed: {
    action(){
      return process.env.VUE_APP_BASE_API + '/admin/file/upload'
    },
    headers(){
      return {
        Authorization:'Bearer ' + this.userinfo.access_token
      }
    },
    ...mapGetters(["userinfo"])
  },
  methods:{
    handleUploaderSuccess(file){
      this.$emit('success',{
        name:file.data.original,
        url:file.data.url
      })
    },
    handleRemove(){
      this.$emit('remove')
    },
    handlePictureCardPreview() {
      this.dialogImgVisible = true;
    }
  }
  
}
</script>

<style lang="scss" scoped>
::v-deep .avatar-uploader{
  line-height: 1;
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    &:hover{
      border-color: #409EFF;
    }
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 110px;
    height: 110px;
    line-height: 110px;
    text-align: center;
  }
  .avatar {
    width: 110px;
    height: 110px;
    display: block;
  }
  .el-upload-list__item-actions {
    position:absolute;
    width:100%;
    height:100%;
    left:0;
    top:0;
    cursor:default;
    text-align:center;
    color:#fff;
    opacity:0;
    font-size:20px;
    background-color:rgba(0, 0, 0, .5);
    -webkit-transition:opacity .3s;
    transition:opacity .3s
}
.el-upload-list__item-actions::after {
      display:inline-block;
      content:"";
      height:100%;
      vertical-align:middle
  }
.el-upload-list__item-actions span {
      display:none;
      cursor:pointer
  }
.el-upload-list__item-actions span+span {
      margin-left:15px
  }
.el-upload-list__item-actions .el-upload-list__item-delete {
      position:static;
      font-size:inherit;
      color:inherit
  }
.el-upload-list__item-actions:hover {
      opacity:1
  }
.el-upload-list__item-actions:hover span {
      display:inline-block
  }
.el-progress {
      top:50%;
      left:50%;
      -webkit-transform:translate(-50%, -50%);
      transform:translate(-50%, -50%);
      bottom:auto;
      width:126px
  }
}
</style>